﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace SimulationPrinterTcpServer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入端口号（数字）。");
            string portStr = Console.ReadLine();
            
            TcpListener tcpListener = new TcpListener(IPAddress.Any, int.Parse(portStr));
            tcpListener.Start();
            Console.WriteLine("服务已启动端口："+ portStr);
            while (true)
            {
                TcpClient client = tcpListener.AcceptTcpClient();//接受一个Client  
                byte[] buffer = new byte[client.ReceiveBufferSize];
                NetworkStream stream = client.GetStream();//获取网络流  

                stream.Read(buffer, 0, buffer.Length);//读取网络流中的数据  

                stream.Close();//关闭流  

                client.Close();//关闭Client  
                string receiveStr = Encoding.Default.GetString(buffer).Trim('\0');//转换成字符串 
                Console.WriteLine("时间：" + DateTime.Now + "\r");
                Console.WriteLine(receiveStr + "\r");
            }
        }
    }
}
